package com.hundunjiang.BlogWebServer.controller;

import cn.hutool.core.util.StrUtil;
import com.hundunjiang.BlogWebServer.server.BlogWebUserService;
import com.hundunjiang.CommonServer.api.CommonResult;
import com.hundunjiang.CommonServer.api.ResultCode;
import com.hundunjiang.CommonServer.client.ToolsClient;
import com.hundunjiang.CommonServer.domain.UserDTO;
import com.hundunjiang.CommonServer.server.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(tags = "web端用户模块")
@RequestMapping("/user")
public class BlogWebUserController {

    @Autowired
    private BlogWebUserService blogWebUserService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ToolsClient toolsClient;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Value("${rabbitmq.SmsExchange}")
    private String SmsExchange;

    @Value("${rabbitmq.SmsRountingKey}")
    private String SmsRountingKey;

    @ApiOperation("账号注册")
    @RequestMapping(path = "/regist",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult regist(
            @ApiParam(name = "username",value = "用户名") @RequestParam("username") String username,
            @ApiParam(name = "password",value = "密码") @RequestParam("password") String password,
            @ApiParam(name = "phone",value = "手机号码") @RequestParam("phone") String phone,
            @ApiParam(name = "nickname",value = "昵称") @RequestParam("nickname") String nickname,
            @ApiParam(name = "code",value = "验证码") @RequestParam("code") String code
    ){
        try{
            if (code.equals(redisService.get(phone))){
                if (blogWebUserService.regist(username+"@hundunjiang.com",password,phone,nickname)){
                    toolsClient.regist(username,password);
                }else {
                    return CommonResult.failed("账户名重复");
                }
            }else {
                return CommonResult.failed("验证码错误");
            }
            //发送注册成功的消息
            rabbitTemplate.convertAndSend(SmsExchange, SmsRountingKey, phone);
            return CommonResult.success(ResultCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    @ApiOperation("手机验证码获取")
    @RequestMapping(path = "/get_phone_code",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult phonecode(
            @ApiParam(name = "phone",value = "手机号码") @RequestParam("phone") String phone
    ){
        try{
            if (blogWebUserService.getPhoneis(phone)){
                return CommonResult.failed("该手机号已被注册");
            }else {
                return toolsClient.get_phone_code(phone);
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }


    @ApiOperation("web端账号登陆")
    @RequestMapping(path = "/userlogin",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult userlogin(
            @ApiParam(name = "username",value = "用户名") @RequestParam("username") String username,
            @ApiParam(name = "password",value = "密码") @RequestParam("password") String password
    ){
        try{
            if(StrUtil.isEmpty(username)||StrUtil.isEmpty(password)){
                return CommonResult.failed("账号或密码不能为空");
            }else {
                return blogWebUserService.userlogin(username+"@hundunjiang.com",password);
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }


    @ApiOperation("获取用户信息")
    @RequestMapping(path = "/loadByUsername",method = RequestMethod.GET)
    @ResponseBody
    @ApiIgnore
    public UserDTO userlogin(
            @ApiParam(name = "username",value = "用户名") @RequestParam("username") String username
    ){
        return blogWebUserService.loadByUsername(username);
    }
}
